<?php

class ViewHelper_Breadcrumb {
	
	/**
	 * @var Zend_Translate_Adapter
	*/
	protected $_translator;
	/**
	 * 翻译
	 * @param string $string
	 * @param string $type
	 * @return string
	*/
	protected function _($string,$options = null){
		$msg = $this->_translator->_($string);
		if(!empty($options) and is_array($options)){
			foreach ($options as $key=>$value){
				$msg = str_replace ( '{'.$key.'}', $value, $msg );
			}
		}
		return $msg;
	}	
	/*
	<div class="breadcrumbs">
	<ul>
		<li><a href="<?php
		echo $this->baseUrl;
		?>"><?php
		echo $this->translate ( 'menu.home' );
		?></a></li>
		<li><a
			href="<?php
			echo $this->baseUrl . '/' . $this->pageInfo->getModuleUrl();
			?>setting/list"><?php
			echo $this->translate ( 'menu.settings.manage' );
			?></a></li>
		<li><?php
		echo $this->translate ( 'menu.settings.city' );
		?></li>
	</ul>
	</div>
	*/
	/*settings_manage.title = "menu.settings.manage"
	settings_manage.url = "#"
	settings_manage.notes.settings_list.title = "menu.settings.list"
	settings_manage.notes.settings_list.url = "setting/list"
	settings_manage.notes.settings_new.title = "menu.settings.new"
	settings_manage.notes.settings_new.url = "setting/new"
	
	*/
	/**
	 * 
	 * @param string $filename
	 * @param Object_PageInfo $pageInfo
	 * @return string
	*/
	public function breadcrumb($filename,$pageInfo,$isBreadCrumbs = true){
		if(!$isBreadCrumbs){
			return "";
		}
		if(empty($this->_translator)){
			$this->_translator = Zend_Registry::get("Zend_Translate");
		}
		$current_url = $pageInfo->getController().'/'.$pageInfo->getAction();
		$savename = str_replace('.','_',$filename);
		$filename = CONFIG_DIR.DIRECTORY_SEPARATOR.'menu'.DIRECTORY_SEPARATOR.$filename;
		$fileObject = Config_Xml::readIni($filename,$savename);
		$html = '';
		if(!empty($fileObject)){
			$menuArray = $fileObject->toArray();
			$id = $this->_getKeyId($menuArray,$current_url);		
			
			$html .= "<div class='breadcrumbs' >";
			$html .= "<ul><li><a href='".$pageInfo->getBaseUrl()."'>";
			$html .= $this->_('menu.home' );
			$html .= "</a></li>";
			if(!empty($id)){
				$keyArray = $menuArray[$id];
				$html .= $this->_getLiHtml($keyArray);
				
				$key = $this->_getNoteId($keyArray,$current_url);
				if(!empty($key)){
					unset($keyArray);
					$keyArray = $menuArray[$id]['notes'][$key];
					$html .= $this->_getLiHtml($keyArray);
				}			
			}
			$html .= "</ul>";
			$html .= "</div>";
		}
		return $html;
	}
	
	protected function _getLiHtml($keyArray){
		$html = "<li>";
		$html .= $this->_($this->_getText($keyArray));
		$html .= "</li>";
		return $html;
	}
	
	protected function _getKeyId($menuArray,$current_url){
		foreach ($menuArray as $id=>$menuValue){
			if($menuValue['url'] == $current_url){
				return $id;
			}
			if(isset($menuValue['notes'])){
				$notesValue = $menuValue['notes'];	
				foreach ($notesValue as $noteValue){
					if($noteValue['url'] == $current_url){
						return $id;
					}
				}
			}
		}
		return null;
	}
	
	protected function _getNoteId($keyArray,$current_url){
		if(isset($keyArray['notes'])){
			$notesValue = $keyArray['notes'];	
			foreach ($notesValue as $key=>$noteValue){
				if($noteValue['url'] == $current_url){
					return $key;
				}
			}
		}
		return null;
	}
	protected function _getText($menuValue){ 
		if(isset($menuValue['title'])){
			return $menuValue['title'];
		}
		$url = isset($menuValue['url'])?$menuValue['url']:'#';
		if($url == '#'){
			throw new Exception('set url text name');
		}
		$url = str_replace('/','.',$url);
		$url = 'menu.'.$url;
		return $url;
	}
	protected function _getHref($menuArray,$url_extend){
		$url = isset($menuArray['url'])?$menuArray['url']:'#';
		$html = "";
		if($url != '' and $url != '#'){
			$url = $url_extend.$url;
			$html .= "href='".$url."'";
		}		
		return $html;
	}
}